import request from '@/utils/request'

export const getChannels = () => {
  return async dispatch => {
    const { data: { channels } } = await request.get('/channels')
    dispatch({ type: 'article/getChannels', payload: channels })
  }
}

export const getArticles = params => {
  return async dispatch => {
    const res = await request.get('/mp/articles', { params })
    const {
      page,
      per_page: pageSize,
      results: list,
      total_count: count
    } = res.data
    dispatch({
      type: 'article/getArticles',
      payload: {
        // 将数据格式化成 Table 需要的格式
        list: list.map(item => {
          return {
            ...item,
            cover: item.cover.images[0]
          }
        }),
        count,
        page,
        pageSize
      }
    })
  }
}